﻿using UnityEditor;
using UnityEngine;

namespace HK.Editor.Extend
{
    /// <summary>
    /// 扩展类 : SerializedProperty
    /// </summary>
    public static class SerializedPropertyEx
    {

        public static int GetDepthLen(this SerializedProperty iProperty)
        {
            if (null == iProperty) return -1;

            var curDepth = iProperty.depth;
            var maxDepth = curDepth;
            var copy = iProperty.Copy();
            var end = copy.GetEndProperty();
            while (copy.NextVisible(true) && !SerializedProperty.EqualContents(copy, end))
            {
                maxDepth = Mathf.Max(maxDepth, copy.depth);
            }

            var depthLen = maxDepth - curDepth;
            return 0 >= depthLen ? 0 : depthLen;
        }
        
        public static int GetVisibleChildCount(this SerializedProperty iProperty)
        {
            if (null == iProperty) return 0;
            var copy = iProperty.Copy();
            var end = copy.GetEndProperty();
            var nextVisible = copy.NextVisible(true);
            var visibleCount = 0;
            while (nextVisible && !SerializedProperty.EqualContents(copy, end))
            {
                ++visibleCount;
                nextVisible = copy.NextVisible(false);
            }

            return visibleCount;
        }

    }
}
